package org.hu.controller;

import org.hu.model.Employee;
import org.hu.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

/**
 * 修改密码
 * @author Htrees
 */
@Controller
public class ChangePasswordController {
    @Autowired
    EmployeeService employeeService;

    /**
     * 修改密码页面
     * @return
     */
    @RequestMapping("/changepassword")
    public String changepassword(){
        return "changepassword";
    }

    /**
     * 执行修改密码
     * @param password 原密码
     * @param newpassword 新密码
     * @param session
     * @param model
     * @return /
     */
    @PostMapping("/dochange")
    public String dochange(String password, String newpassword, HttpSession session, Model model){
        Employee emp = (Employee) session.getAttribute("currentuser");
        Integer result = employeeService.updatepwd(password,newpassword,emp.getUsername(),emp.getPassword());
        if (result == -1){
            model.addAttribute("error","密码输入错误");
            return "forward:/changepassword";
        }else if(result == -2){
            model.addAttribute("error","新密码与旧密码一致");
            return "forward:/changepassword";
        }
        else{
            session.setAttribute("currentuser",null);
            return "redirect:/";
        }
    }


}
